package com.ruoyi.edubuy.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.security.PermissionUtils;
import com.ruoyi.edubuy.domain.ShowBuyOrder;
import com.ruoyi.edubuy.domain.ShowBuyShow;
import com.ruoyi.edubuy.domain.ShowShopping;
import com.ruoyi.edubuy.service.IShowBuyOrderService;
import com.ruoyi.edubuy.service.IShowBuyShowService;
import com.ruoyi.edubuy.service.IShowShoppingService;
import com.ruoyi.tianliangedu.service.ITianliangCourseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Random;

/**
 * @author Lsj
 * @version 1.0
 * @date 2021/7/13 9:23
 */
@Controller
@RequestMapping("/edubuy/show")
public class ShowBuyShowController extends BaseController {
    private String prefix = "edubuy/show";

    @Autowired
    private IShowBuyShowService showBuyShowService;

    @Autowired
    private IShowBuyOrderService showBuyOrderService;


    @Autowired
    private ITianliangCourseService tianliangCourseService;

    @Autowired
    private IShowShoppingService showShoppingService;


    @RequiresPermissions("edubuy:show:view")
    @GetMapping()
    public String order() {
        return prefix + "/show";
    }

    /**
     * 查询商品查询列表
     */
    @RequiresPermissions("edubuy:show:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ShowBuyShow showBuyShow) {
        startPage();
        List<ShowBuyShow> list = showBuyShowService.selectShowBuyShowList(showBuyShow);
        return getDataTable(list);
    }


    @RequestMapping("/buy")
    public String buy(String courseId) {
        System.out.println("用户名是" + (String) PermissionUtils.getPrincipalProperty("userName"));
        ShowBuyOrder showBuyOrder = new ShowBuyOrder();
        // 随机生成订单编号
        showBuyOrder.setOrderId(new Random().nextInt(899999) + 100000);
        showBuyOrder.setOrderName((String) PermissionUtils.getPrincipalProperty("userName"));
        // 获得价格
        showBuyOrder.setOrderPrice(tianliangCourseService.selectTianliangCourseById(Long.valueOf(courseId)).getCoursePrice());
        // 获得商品名字
        showBuyOrder.setOrderCommodity(tianliangCourseService.selectTianliangCourseById(Long.valueOf(courseId)).getCourseName());
        // 将订单生成在数据库
        showBuyOrderService.insertShowBuyOrder(showBuyOrder);
        System.out.println("进入到buy,courseId是:" + courseId);
        return "edubuy/order/order";
    }

    @RequestMapping("buyMore")
    public String buyMore(String courseId) {
        ShowShopping showShopping = new ShowShopping();
        showShopping.setUserId((String) PermissionUtils.getPrincipalProperty("userName"));
        showShopping.setShoppingShop(tianliangCourseService.selectTianliangCourseById(Long.valueOf(courseId)).getCourseName());
        showShopping.setShoppingPrice(tianliangCourseService.selectTianliangCourseById(Long.valueOf(courseId)).getCoursePrice());
        showShoppingService.insertShowShopping(showShopping);
        return "edubuy/show/show";
    }
}
